package com.delivery.www.servlet;

import com.alibaba.fastjson.JSON;
import com.delivery.www.po.Store;
import com.delivery.www.po.User;
import com.delivery.www.constant.Constants;
import com.delivery.www.service.ServiceImpl.StoreServiceImpl;
import com.delivery.www.util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 12970
 */
public class StoreServlet extends BaseServlet {
    StoreServiceImpl storeService = new StoreServiceImpl();

    /**
     * 游客获取所有店铺
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void allStore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String storeName = req.getParameter("storeName");
        String currentPageNo = req.getParameter("currentPageNo");
        boolean flag = false;
        int currentPage = 1;
        int pageSize = 3;
        if (storeName == null) {
            storeName = "";
        }
        if (!"".equals(currentPageNo) && currentPageNo != null) {
            currentPage = Integer.parseInt(currentPageNo);
        }
        List<Store> stores = storeService.getAllStore(storeName, currentPage, pageSize);
        int storeCount = storeService.getStoreCount(storeName);
        int totalPageCount;
        if (storeCount % pageSize == 0) {
            totalPageCount = (storeCount / pageSize);
        } else {
            totalPageCount = (storeCount / pageSize) + 1;
        }

        PageSupport.setCurrentPageNo(currentPage);
        PageSupport.setPageSize(pageSize);
        PageSupport.setTotalCount(storeCount);
        PageSupport.setTotalPageCount(totalPageCount);
        if (currentPage < 1) {
            currentPage = 1;
        } else if (currentPage > totalPageCount) {
            currentPage = totalPageCount;
        }
        req.setAttribute("storeName", storeName);
        req.setAttribute("stores", stores);
        req.setAttribute("currentPageNo", currentPage);
        req.setAttribute("pageSize", pageSize);
        req.setAttribute("totalPageCount", totalPageCount);
        req.setAttribute("flag", flag);
        req.getRequestDispatcher("AllStorePage.jsp").forward(req, resp);
    }

    /**
     * 用户获取所有店铺
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void userStore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String storeName = req.getParameter("storeName");
        String currentPageNo = req.getParameter("currentPageNo");
        boolean flag = false;
        int currentPage = 1;
        int pageSize = 3;
        if (storeName == null) {
            storeName = "";
        }
        if (!"".equals(currentPageNo) && currentPageNo != null) {
            currentPage = Integer.parseInt(currentPageNo);
        }
        List<Store> stores = storeService.getAllStore(storeName, currentPage, pageSize);
        int storeCount = storeService.getStoreCount(storeName);
        int totalPageCount;
        if (storeCount % pageSize == 0) {
            totalPageCount = (storeCount / pageSize);
        } else {
            totalPageCount = (storeCount / pageSize) + 1;
        }

        PageSupport.setCurrentPageNo(currentPage);
        PageSupport.setPageSize(pageSize);
        PageSupport.setTotalCount(storeCount);
        PageSupport.setTotalPageCount(totalPageCount);
        if (currentPage < 1) {
            currentPage = 1;
        } else if (currentPage > totalPageCount) {
            currentPage = totalPageCount;
        }
        req.setAttribute("storeName", storeName);
        req.setAttribute("stores", stores);
        req.setAttribute("currentPageNo", currentPage);
        req.setAttribute("pageSize", pageSize);
        req.setAttribute("totalPageCount", totalPageCount);
        req.setAttribute("flag", flag);
        req.getRequestDispatcher("User/UserStorePage.jsp").forward(req, resp);
    }

    /**
     * 创建
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        String storeName = req.getParameter("storeName");
        String introduce = req.getParameter("introduce");
        String s = storeService.createStore(storeName, user.getUserId(), introduce);
        System.out.println(s);
        writer.print(s);
    }

    public void modify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        String select = req.getParameter("select");
        String value = req.getParameter("value");
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        boolean b;
        if (Constants.SELECT_STORENAME.equals(select)) {
            b = storeService.updateStoreName(storeId, value);
        } else {
            b = storeService.updateStoreIntroduce(storeId, value);
        }
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    public void managerStore(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        List<Store> stores = storeService.getStore();
        writer.print(JSON.toJSONString(stores));
    }

    public void audit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        List<Store> stores = storeService.getAuditStore();
        writer.print(JSON.toJSONString(stores));
    }

    public void status(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        boolean b = storeService.modifyStatus(storeId, 1);
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    public void downStore(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        boolean b = storeService.modifyStatus(storeId, 0);
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }
}
